package com.example.thread.d9CompletableFuture2.d9大数据商品比价;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class ComparePriceService {
    // 方案一：使用串行方式进行商品比价
    public PriceResult getCheapestPlatformPrice(String productName) {
        PriceResult priceResult;
        int discount;
        // 获取淘宝平台的商品价格和优惠
        priceResult = HttpRequest.getTaoBaoPrice(productName);
        discount = HttpRequest.getTaoBaoDiscount(productName);

        PriceResult taoBaoPriceResult = this.computeRealPrice(priceResult, discount);
        // 获取京东平台的商品价格和优惠
        priceResult = HttpRequest.getJDongPrice(productName);
        discount = HttpRequest.getJDongDiscount(productName);

        PriceResult JDPriceResult = this.computeRealPrice(priceResult, discount);

        // 获取拼多多平台的商品价格和优惠
        priceResult = HttpRequest.getPDDPrice(productName);
        discount = HttpRequest.getPDDDiscount(productName);

        PriceResult PDDPriceResult = this.computeRealPrice(priceResult, discount);

        Stream<PriceResult> result = Stream.of(taoBaoPriceResult, JDPriceResult, PDDPriceResult);
        Optional<PriceResult> min = result.min(Comparator.comparing(priceResult1 -> {
            return priceResult1.getRealPrice();
        }));
        PriceResult priceResult1 = min.get();
        return priceResult1;
    }
    // 方案二：使用Future+线程池进行商品比价
    public PriceResult getCheapestPlatformPrice2(String productName) {
        // 线程池
        ExecutorService pool = Executors.newFixedThreadPool(4);
        // 获取淘宝平台商品价格和优惠
        Future<PriceResult> taoBaoFuture = pool.submit(() -> {
            PriceResult priceResult = HttpRequest.getTaoBaoPrice(productName);
            int discount = HttpRequest.getTaoBaoDiscount(productName);
            return this.computeRealPrice(priceResult, discount);
        });
        // 获取京东平台商品价格和优惠
        Future<PriceResult> JDFuture = pool.submit(() -> {
            PriceResult priceResult = HttpRequest.getJDongPrice(productName);
            int discount = HttpRequest.getJDongDiscount(productName);
            return this.computeRealPrice(priceResult, discount);
        });
        // 获取拼多多平台商品价格和优惠
        Future<PriceResult> pddFuture = pool.submit(() -> {
            PriceResult priceResult = HttpRequest.getPDDPrice(productName);
            int discount = HttpRequest.getPDDDiscount(productName);
            return this.computeRealPrice(priceResult, discount);
        });

        // 计算最优平台价格和优惠
        return Stream.of(taoBaoFuture,JDFuture,pddFuture)
                .map(future-> {
                    try {
                        return future.get(5, TimeUnit.SECONDS);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return null;
                    } finally {
                        pool.shutdown();
                    }
                }).filter(Objects::nonNull)
                .min(Comparator.comparing(PriceResult::getRealPrice))
                .get();

    }
    // 方案三：使用CompletableFuture进一步增强
    public PriceResult getCheapestPlatformPrice3(String productName) {
        // 获取淘宝平台商品价格和优惠
        CompletableFuture<PriceResult> taoBaoFuture = CompletableFuture.supplyAsync(() -> {
            return HttpRequest.getTaoBaoPrice(productName);
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            return HttpRequest.getTaoBaoDiscount(productName);
        }), this::computeRealPrice);

        // 获取京东平台商品价格和优惠
        CompletableFuture<PriceResult> JDFuture = CompletableFuture.supplyAsync(() -> {
            return HttpRequest.getJDongPrice(productName);
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            return HttpRequest.getJDongDiscount(productName);
        }), this::computeRealPrice);
        // 获取拼多多平台商品价格和优惠
        CompletableFuture<PriceResult> pDDFuture = CompletableFuture.supplyAsync(() -> {
            return HttpRequest.getPDDPrice(productName);
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            return HttpRequest.getPDDDiscount(productName);
        }), this::computeRealPrice);

        PriceResult priceResult = Stream.of(taoBaoFuture, JDFuture, pDDFuture)
                .map(CompletableFuture::join)
                .min(Comparator.comparing(PriceResult::getRealPrice))
                .get();
        return priceResult;
    }

    public PriceResult batchComparePrice(List<String> products) {
        // 遍历每个商品的名字，根据商品名称开启异步任务获取最终价，归集到list集合中
        List<CompletableFuture<PriceResult>> futures = products.stream().map(productName -> {
            return CompletableFuture.supplyAsync(() -> HttpRequest.getTaoBaoPrice(productName))
                    .thenCombine(CompletableFuture.supplyAsync(() -> HttpRequest.getTaoBaoDiscount(productName)), (this::computeRealPrice));
        }).collect(Collectors.toList());

        // 多个商品的最终价进行排序获取最小值
       return futures.stream()
                .map(CompletableFuture::join)
                .sorted(Comparator.comparing(PriceResult::getRealPrice))
                .findFirst()
                .get();
    }


        // 获取商品最终价格
    public PriceResult computeRealPrice(PriceResult priceResult,int disount) {
        priceResult.setRealPrice(priceResult.getPrice()-disount);
        priceResult.setDiscount(disount);
        CommonUtilsPlus.printThreadLog(priceResult.getPlatform()+"最终价格计算完成："+priceResult.getRealPrice());
        return priceResult;
    }
}
